//
// Created by monoliths on 8/30/22.
//

#ifndef IMAGEPROCESSINGLIB_HTTPSTREAMRECEIVE_H
#define IMAGEPROCESSINGLIB_HTTPSTREAMRECEIVE_H

#include <string>
#include <functional>
#include <utility>



class HttpStreamReceive
{
private:
    /**
     * the host name for request
     */
    std::string m_host;

    /**
     * server port
     */
    uint16_t m_port;

    /**
     * the data for received
     */
    char* m_pData;


    int m_socketFD = -1;

    std::string m_requestHeader;

    /**
     *
     * @return
     */
//    int readLineFromSocket(char*, int) const;
//
//    int readBytesFromSocket(char*,int);

protected:

    /**
     * the callback for received part data;
     */
    std::function<bool(char*, size_t)> m_partReceivedCallback;

public:
    HttpStreamReceive() : m_host("0.0.0.0"), m_port(80), m_pData(nullptr) {};
//    explicit HttpStreamReceive(std::string  host_, uint16_t port_) : m_host(std::move(host_)), m_port(port_), m_pData(nullptr) {};
    explicit HttpStreamReceive(std::string&&  host_, uint16_t port_) : m_host(host_), m_port(port_), m_pData(nullptr) {};
    explicit HttpStreamReceive(const std::string&  host_, uint16_t port_) : m_host(host_), m_port(port_), m_pData(nullptr) {};

    const std::string& requestHeader();
    void setRequestHeader(std::string&&);

    uint16_t& port();
    void setPort(uint16_t);

    /**
     * start the request
     */
    void startRequestStream();

    /**
     * set the callback function
     */
    void setPartReceivedCallback(const std::function<bool(char*, size_t)>&);

    /**
     * get callback
     * @return callback
     */
    std::function<bool(char*, size_t)>&& partReceivedCallback();

    int readLineFromSocket(int fd, char *recvData, int recvLength) const;

    int readBytesFromSocket(int fd, char *recvData, int recvLength) const;
};

#endif //IMAGEPROCESSINGLIB_HTTPSTREAMRECEIVE_H